package com.wuge.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


import com.wuge.gulimall.product.entity.AttrEntity;
import com.wuge.gulimall.product.service.AttrAttrgroupRelationService;
import com.wuge.gulimall.product.service.AttrService;
import com.wuge.gulimall.product.service.CategoryService;
import com.wuge.gulimall.product.vo.AttrGroupRelactionVo;
import com.wuge.gulimall.product.vo.AttrGroupWithAttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wuge.gulimall.product.entity.AttrGroupEntity;
import com.wuge.gulimall.product.service.AttrGroupService;
import com.wuge.common.utils.PageUtils;
import com.wuge.common.utils.R;



/**
 * 属性分组
 *
 * @author ñÒ¹ÚÎé
 * @email 1712378864@qq.com
 * @date 2021-06-27 14:36:04
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;
    @Autowired
    private AttrService attrService;
//    /product/attrgroup/{catelogId}/withattr
@RequestMapping("/{catelogId}/withattr")
public R getlistAttrGroupwithattr(@PathVariable("catelogId") Long catelogId){
    List<AttrGroupWithAttrVo> attrGroupWithAttrVos = attrGroupService.getlistAttrGroupwithattr(catelogId);

    return R.ok().put("data", attrGroupWithAttrVos);
}
    /**
     * 列表
     */
    @RequestMapping("/list/{cateId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("cateId") Long cateId){
        PageUtils page = attrGroupService.queryPage(params,cateId);

        return R.ok().put("page", page);
    }
    @PostMapping("/attr/relation")
    public R update(@RequestBody List<AttrGroupRelactionVo> attrGroupRelactionVos){
        attrAttrgroupRelationService.update(attrGroupRelactionVos);



        return R.ok();
    }

    @RequestMapping("/{attrgroupId}/attr/relation")
    //@RequiresPermissions("product:attrgroup:list")
    public R getAttr(@PathVariable("attrgroupId") Long cateId){

        List<Long> attrIds = attrAttrgroupRelationService.getAttrIds(cateId);
        if (attrIds==null||attrIds.size()==0){
            return R.ok().put("data", null);
        }
        List<AttrEntity> attrEntities = attrService.listByIds(attrIds);

        return R.ok().put("data", attrEntities);
    }
    @RequestMapping("/{attrgroupId}/noattr/relation")
    //@RequiresPermissions("product:attrgroup:list")
    public R getNoAttr(@RequestParam Map<String, Object> params,@PathVariable("attrgroupId") Long cateId){

        PageUtils noAttr = attrAttrgroupRelationService.getNoAttr(cateId, params);

        return R.ok().put("page", noAttr);
    }
    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
  //  @RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
   attrGroup.setCatelogIds(categoryService.parentId(attrGroupId));
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
   // @RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
   // @RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
   // @RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }
    @RequestMapping("/attr/relation/delete")
    // @RequiresPermissions("product:attrgroup:delete")
    public R deletes(@RequestBody AttrGroupRelactionVo[] attrGroupRelactionVos){
        attrAttrgroupRelationService.deleteRelation(attrGroupRelactionVos);

        return R.ok();
    }

}
